// pages/feedback/index.js
const {
  checkContent,
  uploadPic,
  uploadImage,
  cutBase64,
  checkImg,
  checkLogin
} = require('../../../utils/util')
const {
  dictData,
  feedback
} = require('../../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginVisible: true,
    selectData: {
      title: '请选择问题类型',
      key: 'school',
      items: [{
          name: '厦门校区'
        },
        {
          name: '泉州校区'
        },
      ]
    },
    showSelect: false,
    type: null,
    queIndex: null,
    quesOptions: [{
        title: '产品建议',
        id: 1
      },
      {
        title: '功能异常',
        id: 2
      },
      {
        title: '服务投诉',
        id: 3
      }, {
        title: '其它问题',
        id: 4
      },

    ],
    picsMap: [],
    photoList: [],
    maxLength: 3
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },
  async fetchData() {
    const {
      result
    } = await dictData('advice_type')
    this.setData({
      quesOptions: result
    })
  },
  bindPickerChange(e) {
    const index = parseInt(e.detail.value)
    this.setData({
      queIndex: index,

    })
  },
  choose() {
    this.setData({
      showSelect: true
    })
  },
  confirm(e) {
    this.setData({
      type: e.detail.item,
    })
  },
  chooseImage(e) {
    var that = this;
    const type = e.currentTarget.dataset.type
    var {
      picsMap,
      maxLength
    } = this.data
    var count = maxLength - picsMap.length
    uploadImage(count).then((filePath) => {
      that.setData({
        photoList: filePath
      })
      that.upload()
      // cutBase64(filePath, 'canvas', 0, that.setCanvas, that.savePic);
    })
  },
  setCanvas(width, height, callback) {
    this.setData({
      cWidth: width,
      cHeight: height
    }, callback);
  },
  savePic(url, index) {
    const {
      photoList,
      images
    } = this.data
    images.push(url)
    this.setData({
      images
    })
    if (images.length == photoList.length) {
      this.complete(images)
    }
  },

  complete(images) {
    //上传图片到云存储


    let promiseArr = [];
    const that = this;
    var a = 0
    for (var i = 0; i < images.length; i++) {
      //正则表达式返回文件的扩展名
      let item = images[i];
      let suffix = /\.\w+$/.exec(item)[0];
      wx.cloud.uploadFile({
        cloudPath: '动态图片' + new Date().getTime() + suffix, // 上传至云端的路径
        filePath: item, // 小程序临时文件路径
        success: res => {
          wx.cloud.callFunction({
            name: 'checkImg',
            data: {
              contentType: 'image/jpg',
              fileID: res.fileID
            }
          }).then(data => {
            if (data.result.errCode !== 0) {
              wx.hideLoading()
              wx.showToast({
                title: '图片内容不符合审核要求',
                icon: 'none'
              })
              that.setData({
                images: [],
                photolist: []
              })
              return
            }
            a = a + 1
            if (a == images.length) {
              that.upload()
            }
          })
        },
        fail: res => {
          wx.hideLoading();
          wx.showToast({
            title: "上传失败",
          })
        }
      })

    }

  },
  async submit(e) {
    const {
      content
    } = e.detail.value
    const {
      quesOptions,
      queIndex,
      picsMap
    } = this.data
    if (typeof queIndex !== 'number') {
      wx.showToast({
        title: '请选择问题类型',
        icon: 'none'
      })
      return
    }
    if (!content) {
      wx.showToast({
        title: '请输入问题描述',
        icon: 'none'
      })
      return
    }
    const params = {
      content,
      type: quesOptions[queIndex].value,
      picUrl: picsMap.join(',')
    }
    const {
      result,
      message
    } = await feedback(params)
    wx.showToast({
      title: message,
      icon: 'none'
    })
    setTimeout(() => {
      wx.navigateBack()
    }, 500);
  },
  async upload() {
    var _this = this
    const {
      picsMap,
      photoList
    } = this.data;

    for (let i = 0; i < photoList.length; i++) {
      const img = await uploadPic(photoList[i])
      picsMap.push(img)
    }
    _this.setData({
      picsMap,
      images: [],
      photolist: []
    })
    wx.hideLoading()
  },
  previewImg(e) {
    getApp().globalData.preview = false;
    var src = e.currentTarget.dataset.url //获取data-src  循环单个图片链接

    // var str = this.data.details.imgList; //获取data-effect_pic   图片列表
    var str = this.data.picsMap
    //图片预览
    wx.previewImage({
      current: src,
      // 当前显示图片的http链接
      urls: str // 需要预览的图片http链接列表
    });
  },
  delPhoto(e) {
    const picsMap = this.data.picsMap;
    if (picsMap.length) {
      const id = e.currentTarget.dataset.id;
      picsMap.splice(id, 1);
      this.setData({
        picsMap
      })
    }
  },
})